package com.lik.littleworld.web.service.impl;

import cn.yiban.util.AESDecoder;
import com.google.gson.Gson;
import com.lik.littleworld.common.auth.AuthResponse;
import com.lik.littleworld.web.exception.YiBanAuthException;
import com.lik.littleworld.web.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AuthServiceImpl implements AuthService {

    @Autowired
    private Gson gson;

    @Override
    public AuthResponse requestUserInfo(String verifyRequest, String appSecret, String appId) {
        AuthResponse authResponse;
        try {
            String userInfo = dec(verifyRequest, appSecret, appId).trim();
            authResponse = gson.fromJson(userInfo, AuthResponse.class);
        }catch (Exception e){
            throw new YiBanAuthException("解析授权信息失败");
        }
        return authResponse;
    }

    private String dec(String targetText, String appSecert, String appId) {
        String dec = "";
        try {
            dec = AESDecoder.dec(targetText, appSecert, appId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dec;
    }
}
